<?php
namespace  Teachers\Controller;
use Think\Controller;

class IndexController extends CommonController
{

	public function index()
	{
		$this->display();
	
	}
	public function top()
	{
		$this->display();
	}
	public function left()
	{
		$this->display();
	}
	public function left2()
	{
		$this->display();
	}
	public function left3()
	{
		$this->display();
	}
	public function left4()
	{
		$this->display();
	}
	public function left5()
	{
		$this->display();
	}
	/**
	 * 获取当前教师用户所在学校各项类别信息
	 * param school_id 学校ID
	 * param category_id 一级分类ID
	 * param pid	子分类ID
	 * */
	public function showSchoolArticle($school_id,$category_id)
	{
//		$uid=$_SESSION['onethink_home']['user_auth']['uid'];	//获取当前用户ID
//		$school_id=$_SESSION['onethink_home']['user_auth']['school_id'];	//获取当前用户ID	
            
		$document=D('Document');
		$category_id=$category_id;
		$pid=I('get.pid',1);
		$map['school_id']=$school_id;
		$map['category_id']=$category_id;
		$map['pid']=$pid;
		$document_data=$document->where($map)->select(); 
	
		$this->assign("document_data",$document_data);
		$this->display();
	
	}
	 
	/**
	 * 新增学校各项类别信息
	 * 
	 * */
	public function addSchoolArticle()
	{	$school_id=$this->showschoolID();
		if($school_id && I('post.category_id'))
		{	
			if(I('post.pid'))
			{
				$document=M('document');
				$article=M('document_article');
                                
				$data = array(									//document表更新数据
					'school_id'		=>$school_id,		//学校ID
					'title'			=> I('post.title'),			//文档标题
					'description'	=> I('post.description'),	//文档描述
					'category_id'	=> I('post.category_id'),	//文档分类ID
					'model_id'		=>I('post.model_id'),	 	//内容模型ID
					'type'			=>I('post.type'),	 		//内容类型ID
					'link_id'		=> I('post.link_id'),	 	//链接ID
					'level'			=> I('post.level'),	 		//级别
					'create_time'	=> I('post.create_time'),	//创建时间
					'update_time'	=> I('post.update_time'),	//更新时间
					'status'		=> I('post.status'),		//状态位
					'pid'			=> I('post.pid'),			//子分类ID
					'status'		=> I('post.status',1),
				);
				$article_id=$document->add($data);
				if($article_id)
				{
					$contentdata = array(		
						'id'		=> $article_id,
						'parse'		=> I('post.parse'),		//解析类型
						'content'	=> I('post.content'),		//文章内容
						'template'	=> I('post.template'),		//详情页显示模板
						'bookmark'	=> I('post.bookmark'),		//文档收藏数
					);
					$articleflag=$article->add($contentdata);
					if($articleflag)
					{
						$this->error('“'.I('post.title').'” 新增失败！');
					}
				}
			}
		}
	}
	
	
	/**
	 *更新学校各项类别信息
	 * 
	 * */
	public function updateSchoolArticle()
	{
		$document=M('document');
		$article=M('document_article');
		$school_id=$this->showschoolID();
		if(!empty($school_id) && !empty($_POST['article_id']))
		{
			$where['school_id']=$school_id;
			$where['category_id']=$_POST['category_id'];
			$where['pid']=$_POST['pid'];
			$article_where['id']=$_POST['article_id'];
			$data = array(									//document表更新数据
					'school_id'		=>$school_id,		//学校ID
					'title'			=> I('post.title'),			//文档标题
					'description'	=> I('post.description'),	//文档描述
					'category_id'	=> I('post.category_id'),	//文档分类ID
					'model_id'		=>I('post.model_id'),	 	//内容模型ID
					'type'			=>I('post.type'),	 		//内容类型ID
					'link_id'		=> I('post.link_id'),	 	//链接ID
					'level'			=> I('post.level'),	 		//级别
					'create_time'	=> I('post.create_time'),	//创建时间
					'update_time'	=> I('post.update_time'),	//更新时间
					'status'		=> I('post.status'),		//状态位
					'pid'			=> I('post.pid'),			//子分类ID
					'status'		=> I('post.status',1),
				);
			$contentdata = array(		
						'id'		=> $article_id,
						'parse'		=> I('post.parse'),		//解析类型
						'content'	=> I('post.content'),		//文章内容
						'template'	=> I('post.template'),		//详情页显示模板
						'bookmark'	=> I('post.bookmark'),		//文档收藏数
			);
			$flag=$document->where($where)->save($data);			//更新document表
			if($flag)
			{
				$article->where($article_where)->save($contentdata);	//更新document_article表
			}else{
				$this->error ('“'.$_POST['title'].'” 更新失败！');
			}
		}
		
	}
	
	
	 /**
	  * 删除各项类别信息
	  *  假删除 更新状态位为-1
	  **/
	public function deleteSchoolArticle()
	{
		$article_id=I('post.id',0);
		if(!empty($article_id) && $article_id>0)
		{
			$document=M('document');
			$where['id']=$article_id;
			$data=array
			(
				'status' => '-1'
			);
			if(!($document->where($where)->save($data)))
			{
				$this->error('删除失败！');
			}
			
			
//			if($document->where($where)->delete())
//			{
//				$article->where($where)->delete();
//			}
//			else
//			{
//				$this->error = '删除失败！';
//			}
			
		}
		
		
	}

}